//=============================================================================
#include "AppStateMenu.h"
#include "AppStateManager.h"
#include "App.h"
#include "Random.h"
#include "Items.h"
//=============================================================================
CAppStateMenu CAppStateMenu::Instance;

//=============================================================================
CAppStateMenu::CAppStateMenu() {
	choice = 0;
	IP = "";
	port = "";
	KeyInput = false;
	fullscreen = false;
	screenChoice = false;
	writeIP = false;
	MAX_IP_SIZE = 15;
	mainChoice = 0;
	optionsText = "FullScreen off";
	
	
}

//=============================================================================
void CAppStateMenu::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
}

//------------------------------------------------------------------------------
void CAppStateMenu::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) {
	
	//The code to insert IP into a string.
	switch(mainChoice)
	{
		case JOIN_CHOICE:     OnKeyIpWrite(sym);
			break;
		case PORT_CHOICE:	  OnKeyPortWrite(sym);
			break;
		//Options
		case OPTIONS_CHOICE:  OnKeyOptions(sym);
			break;
		//Control edit
		case CONTROL_CHOICE:  OnKeyOptionsControls(sym);
			break;
		//Customize
		case NAME_CHOICE:	  OnKeyNameWrite(sym);
			break;
		//Main menu
		case MAINMENU_CHOICE: OnKeyMainMenuChoice(sym);
			break;
	}

}

//=============================================================================
void CAppStateMenu::OnActivate() {

	//Initialize surfaces
	menuBG = CSurface::OnLoad("./images/menu/menuBG.png");
	OptionBox = CSurface::OnLoad("./images/menu/OptionScreen.png");
	IPBox = CSurface::OnLoad("./images/menu/IPBox.png");
	NameBox = CSurface::OnLoad("./images/menu/NameBox.png");
	sky = CSurface::OnLoad("./images/menu/Cloud.png");
	spell = CSurface::OnLoad("./images/menu/fireball.png");
	ControlsBox = CSurface::OnLoad("./images/InGameGUI/HelpScreen.png");

	if(menuBG == NULL || IPBox == NULL || sky == NULL || spell == NULL || OptionBox == NULL)
		return;

	//Set random coordinates to sky and spell animation images.
	sky1.x = Random(0, 640);
	sky1.y = Random(0, 280);
	sky2.x = Random(0, 640);
	sky2.y = Random(0, 280);
	sky3.x = Random(0, 640);
	sky3.y = Random(0, 280);
	fire1.y = Random(0, 300);
	fire2.y = Random(0, 280);

	//Set itemstats to first item.
	stats[0] = "";
	stats[1] = "";
	stats[2] = "";

	std::string effects =  itemInstance.effects[0];
	
				
	stats[0]+= effects.at(0);
	stats[0]+= effects.at(1);
	stats[0]+= effects.at(2);
							
	stats[1]+= effects.at(4);
	stats[1]+= effects.at(5);
	stats[1]+= effects.at(6);

	stats[2]+= effects.at(8);
	stats[2]+= effects.at(9);
	stats[2]+= effects.at(10);

	//initialize fonts.
	if( TTF_Init() == -1 )
        return;    

	menuChoiceFont = TTF_OpenFont("FreeSans.ttf", 38);
	mainTextfont   = TTF_OpenFont("FreeSans.ttf", 68);
	IPFont		   = TTF_OpenFont("FreeSans.ttf", 24);

	if (IPFont == NULL || menuChoiceFont == NULL || mainTextfont == NULL)
		return;

	//Add menu sound and music effects
	if(CSoundBank::SoundControl.SoundList.size() == NULL)
		CSoundBank::SoundControl.OnLoad("./Sounds/music01.ogg", 0);
			

	CSoundBank::SoundControl.OnLoad("./Sounds/effect01.ogg", 1);

}

//-----------------------------------------------------------------------------
void CAppStateMenu::OnDeactivate() {
	
	if(menuBG) {
        SDL_FreeSurface(menuBG);
		SDL_FreeSurface(OptionBox);
		SDL_FreeSurface(IPBox);
		SDL_FreeSurface(NameBox);
		SDL_FreeSurface(sky);
		SDL_FreeSurface(spell);
		SDL_FreeSurface(ControlsBox);
	    menuBG = NULL;
		OptionBox = NULL;
		IPBox = NULL;
		NameBox = NULL;
		sky = NULL;
		spell = NULL;
		ControlsBox = NULL;
    }
}

//-----------------------------------------------------------------------------
void CAppStateMenu::OnLoop() {
	
	OnLoopAnimation();
}

//-----------------------------------------------------------------------------
void CAppStateMenu::OnRender(SDL_Surface* Surf_Display) {
	
	OnRenderMainWindow(Surf_Display);

	
	if(mainChoice == JOIN_CHOICE || mainChoice == PORT_CHOICE)    
		OnRenderJoin(Surf_Display);
	else if(mainChoice == NAME_CHOICE)
		OnRenderNameChoice(Surf_Display);
	else if(mainChoice == OPTIONS_CHOICE)
		OnRenderOptions(Surf_Display);
	else if(mainChoice == CONTROL_CHOICE)
		OnRenderControls(Surf_Display);

}

//=============================================================================
CAppStateMenu* CAppStateMenu::GetInstance() {
	return &Instance;
}

//=============================================================================
